source isFileEmpty.sh
source ~/.bashrc
if  test ! -s "$1" ; then
    echo "This shell qsubs jobs to the pipe line to enumerate CDSs"
    echo "and estimate their expression level." 
    echo "The only ptr is the file name of a gene list:"
    echo "Each line of the list has the following info of one gene:"
    echo "#name   chrom   strand  txStart txEnd, given by the UCSC genome browser." 
    exit
fi

# one job for each chromosome 
function qsubBatchRunEnumIPerMTranSEQ {
    geneListFileName=$1
    tmpShellName=$1'_qsub.sh'
    echo "#!/bin/bash" > $tmpShellName
    {
        echo "#PBS -l walltime=72:00:00"
        echo "#PBS -l mem=4g"
        echo "#PBS -l pmem=4g"
        echo "#PBS -l nodes=1:ppn=1"
        echo "#PBS -l file=80g"
        echo "#PBS -q cmb"
        echo "cd $PWD"
        echo "~/SHELL/batchRunEnumIPerMTranSEQ.sh $geneListFileName"
    } >> $tmpShellName    
    echo "~/SHELL/batchRunEnumIPerMTranSEQ.sh $geneListFileName"
    qsub ./$tmpShellName
    \rm $tmpShellName
}

qsubBatchRunEnumIPerMTranSEQ $1
exit

# one job for each gene
function qsubRunEnumIPerMTranSEQ {
    gene=$1
    chr=$2
    gStart=$3
    gEnd=$4
    tmpShellName=$gene'_qsub.sh'    
    echo "#!/bin/bash" > $tmpShellName
    {
        echo "#PBS -l walltime=72:00:00"
        echo "#PBS -l mem=4g"
        echo "#PBS -l pmem=4g"
        echo "#PBS -l nodes=1:ppn=1"
        echo "#PBS -l file=4g"
        echo "#PBS -q cmb"
        echo "cd $PWD"
        echo "~/SHELL/runEnumIPerMTranSEQ.sh $gene $chr $gStart $gEnd"
    } >> $tmpShellName    
    echo "run runEnumIPerMTranSEQ.sh with $gene $chr $gStart $gEnd in $PWD"
    #qsub ./$tmpShellName
    \rm $tmpShellName
}

exec < $1
while read line
do
    if [[ $(expr match $1 "#") == 1 ]]; then
       continue   # comment 
    fi
    # echo will turn tab into space automatically
    gene=$(echo $line | cut -f 1 -d' ')
    chr=$(echo $line | cut -f 2 -d' ')
    strand=$(echo $line | cut -f 3 -d' ')
    gStart=$(echo $line | cut -f 4 -d' ')
    gEnd=$(echo $line | cut -f 5 -d' ')
    if [ $gEnd -le $gStart ]
    then
        tmp=$gEnd
        gEnd=$gSTart
        gStart=$gEnd
    fi
    # This is the only line different then batchRunEnumIPerMTranSEQ.sh 
    qsubRunEnumIPerMTranSEQ $gene $chr $gStart $gEnd
done
#\rm *qsub.sh
exit
